/**
 * Created by Mark_ren on 2016/10/25.
 * 生活本不应如此坎坷
 */
var express = require('express');
var router = express.Router();
var co = require('co');
var seqModule = require('./createModules');
var seq = new seqModule();

//点赞
router.get('/addLike', function(req, res, next) {
    co(function () {
        req.createTime = decodeURI(req.query.createTime);
        seq.likedModule.create(req.query).then(function(results){
            res.json({"success":true,"rows":results});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'添加失败'});
        console.log(e);
    })
});
//取消赞(删除)
router.get('/deleteLike', function(req, res, next) {
    co(function () {

        seq.likedModule.destroy({'where':{'id':id}}).then(function(results){
            res.json({"success":true,"msg":"取消成功","rows":results});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'取消失败'});
        console.log(e);
    })
});
/**
 * /PC数据统计.返回三模块的点赞收藏数)/
 * type:1001 大课堂
 *      1002 分享会
 *      1003 新闻
 */
router.get('/getLikes', function(req, res, next) {
    co(function () {
        var params="";
        var page=parseInt(req.query.page);
        var rows=parseInt(req.query.rows);
        var total=0;
        if(req.query.type!=null&&req.query.type!=''&&req.query.type!=0){
            params=" and n.type ="+req.query.type;
        }
        if(req.query.title!=null&&req.query.title!=''){
            params+=" and n.title like '%"+req.query.title+"%'";
        }
        seq.sequelize.query("select count(id) as total from ( select * ,(select count(id) from qcc_collection where n.id = course_id  LIMIT 1) as shoucang,(select count(id) " +
            "  from qcc_liked where n.id=course_id LIMIT 1) as dianzan  from (select id,title, CREATETIME ,1001 as type" +
            "  from tp_ccdt.qcc_classroom  union all select id,title, CREATETIME , 1002 as type   from tp_ccdt.qcc_discuss union ALL" +
            "  select id,title, CREATETIME , 1003 as type from tp_ccdt.qcc_news  )  as n " +
            "where 1=1 "+params+" order by n.createtime) as q limit 1  ").spread(function(results){
            total=results[0].total;
        });
        seq.sequelize.query("select * ,(select count(id) from qcc_collection where n.id = course_id  LIMIT 1) as shoucang,(select count(id) " +
            "  from qcc_liked where n.id=course_id LIMIT 1) as dianzan  from (select id,title, CREATETIME ,1001 as type" +
            "  from tp_ccdt.qcc_classroom  union all select id,title, CREATETIME , 1002 as type   from tp_ccdt.qcc_discuss union ALL" +
            "  select id,title, CREATETIME , 1003 as type from tp_ccdt.qcc_news  )  as n " +
            "where 1=1 "+params+" order by n.createtime limit "+page*rows+","+(page+1)*rows+";").spread(function(result){

            res.json({"success":true,"page":page,"total":total,"result":result});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'查询失败'});
        console.log(e);
    })
});



module.exports = router;